package com.meituan.sqt.mock.application.web;

import com.meituan.sqt.mock.domain.SecretKeyService;
import com.meituan.sqt.mock.domain.modelC.TypeCThirdPayService;
import com.meituan.sqt.request.thirdpay.ThirdPayQueryRequest;
import com.meituan.sqt.request.thirdpay.ThirdPayRequest;
import com.meituan.sqt.request.thirdpay.ThirdRefundRequest;
import com.meituan.sqt.response.BaseApiResponse;
import com.meituan.sqt.response.thirdpay.ThirdPayQueryRespBody;
import com.meituan.sqt.response.thirdpay.ThirdPayRespBody;
import com.meituan.sqt.response.thirdpay.ThirdRefundRespBody;
import com.meituan.sqt.utils.EncryptUtil;
import com.meituan.sqt.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author: chenzhenfeng@meituan.com
 * @date: 2023/01/28 11:33
 * @description:
 * 客户平台数据模型设计时C类模型：
 *        C类模型（称为支付流水模型）     存在支付表和退款表，通过商企通侧交易号串起用户的支付和退款动作，关注用户是否多次唤起收银台，不关注实际订单信息
 *
 * 客户平台和商企通进行支付网关对接时，可以此参考流程实现服务端
 */

@Slf4j
@Controller
@RequestMapping("/modelC/thirdPay")
public class TypeCThirdPayController {
    @Resource
    SecretKeyService secretKeyService;
    @Resource
    TypeCThirdPayService thirdPayService;

    /**
     * 【下单接口】接收美团商企通发起的下单请求
     * @return
     */
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse<String> mockPay(String token, String version, String content) {
        log.info("【商企通标准三方-下单】token:{}, version:{}, 密文content:{}", token, version, content);
        try {
            String secretKey = secretKeyService.getSecretKey(token);
            String decryptContent = EncryptUtil.aesDecrypt(content, secretKey);
            log.info("【商企通标准三方-下单】token:{}, 明文decryptContent:{}", token, decryptContent);
            // 1.获取请求
            ThirdPayRequest request = JsonUtil.json2Object(decryptContent, ThirdPayRequest.class);

            // 2.第三方平台的业务逻辑
            ThirdPayRespBody respBody = thirdPayService.pay(request);

            log.info("【商企通标准三方-下单】request:{}, response:{}", JsonUtil.object2Json(request), JsonUtil.object2Json(respBody));
            return BaseApiResponse.success(EncryptUtil.aesEncrypt(JsonUtil.object2Json(respBody), secretKey));
        } catch (Exception e) {
            log.error("【商企通标准三方-下单】content:{}, 处理异常", content, e);
            return BaseApiResponse.fail(e.getMessage());
        }
    }

    /**
     * 【退款接口】接收美团商企通发起的退款请求
     * @return
     */
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse<String> mockRefund(String token, String version, String content) {
        log.info("【商企通标准三方-退款】token:{}, version:{}, 密文content:{}", token, version, content);
        try {
            String secretKey = secretKeyService.getSecretKey(token);
            String decryptContent = EncryptUtil.aesDecrypt(content, secretKey);
            log.info("【商企通标准三方-退款】token:{}, 明文decryptContent:{}", token, decryptContent);
            // 1.获取请求
            ThirdRefundRequest request = JsonUtil.json2Object(decryptContent, ThirdRefundRequest.class);
            // 2.第三方平台的业务逻辑
            ThirdRefundRespBody respBody = thirdPayService.refund(request);

            log.info("【商企通标准三方-退款】request:{}, response:{}", JsonUtil.object2Json(request), JsonUtil.object2Json(respBody));
            return BaseApiResponse.success(EncryptUtil.aesEncrypt(JsonUtil.object2Json(respBody), secretKey));
        } catch (Exception e) {
            log.error("【商企通标准三方-退款】content:{}, 处理异常", content, e);
            return BaseApiResponse.fail(e.getMessage());
        }
    }

    /**
     * 【支付查询接口】接收美团商企通发起的支付查询请求
     *  第三方平台在用户完成支付后，应该通过【支付成功通知接口】将用户支付成功消息通知给美团商企通，美团商企通可以通过【支付查询接口】主动查询订单的支付状态
     * @return
     */
    @RequestMapping(value = "/payQuery", method = RequestMethod.POST)
    @ResponseBody
    public BaseApiResponse<String> mockQueryPayStatus(String token, String version, String content) {
        log.info("【商企通标准三方-支付状态查询】token:{}, version:{}, 密文content:{}", token, version, content);
        try {
            String secretKey = secretKeyService.getSecretKey(token);
            String decryptContent = EncryptUtil.aesDecrypt(content, secretKey);
            log.info("【商企通标准三方-支付状态查询】token:{}, 明文decryptContent:{}", token, decryptContent);
            // 1.获取请求
            ThirdPayQueryRequest request = JsonUtil.json2Object(decryptContent, ThirdPayQueryRequest.class);
            // 2.第三方平台的业务逻辑
            ThirdPayQueryRespBody respBody = thirdPayService.payQuery(request);
            log.info("【商企通标准三方-支付状态查询】request:{}, response:{}", JsonUtil.object2Json(request), JsonUtil.object2Json(respBody));
            return BaseApiResponse.success(EncryptUtil.aesEncrypt(JsonUtil.object2Json(respBody), secretKey));
        } catch (Exception e) {
            log.error("【商企通标准三方-支付状态查询】content:{}, 处理异常", content, e);
            return BaseApiResponse.fail(e.getMessage());
        }
    }
}
